import { GoodsIssues } from './goodsIssue';
let _GoodsIssues = GoodsIssues;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import GoodsIssueMock from './mallm/goods/goodsIssueMock';
 * GoodsIssueMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mallm/goods/goodsIssue/listKey').reply(config => {
      let params = config.params;
      let mockGoodsIssues = _GoodsIssues.filter(goodsIssue => {
      	if(params.question||params.question==''){ if( goodsIssue.question.indexOf(params.question)>=0) return true;}
      	if(params.answer||params.answer==''){ if( goodsIssue.answer.indexOf(params.answer)>=0) return true;}
      	if(params.goodsId||params.goodsId==''){ if( goodsIssue.goodsId.indexOf(params.goodsId)>=0) return true;}
      	if(params.id||params.id==''){ if( goodsIssue.id.indexOf(params.id)>=0) return true;}
      	if(params.deptid||params.deptid==''){ if( goodsIssue.deptid.indexOf(params.deptid)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockGoodsIssues
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mallm/goods/goodsIssue/list').reply(config => {
      let params = config.params;
      let mockGoodsIssues = _GoodsIssues.filter(goodsIssue => {
      	var isTrue=false;
      	if(params.question||params.question==''){if(goodsIssue.question.indexOf(params.question)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.answer||params.answer==''){if(goodsIssue.answer.indexOf(params.answer)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.goodsId||params.goodsId==''){if(goodsIssue.goodsId.indexOf(params.goodsId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.id||params.id==''){if(goodsIssue.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.deptid||params.deptid==''){if(goodsIssue.deptid.indexOf(params.deptid)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockGoodsIssues
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockGoodsIssues.length;
      mockGoodsIssues = mockGoodsIssues.filter((goodsIssue, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockGoodsIssues,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mallm/goods/goodsIssue/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _GoodsIssues = _GoodsIssues.filter(goodsIssue => goodsIssue.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mallm/goods/goodsIssue/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _GoodsIssues = _GoodsIssues.filter(goodsIssue => !params.includes(goodsIssue.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mallm/goods/goodsIssue/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { question,answer,goodsId,id,deptid } = params;
      _GoodsIssues.some(goodsIssue => {
        if (goodsIssue.id === id) {
			goodsIssue.question = question;
			goodsIssue.answer = answer;
			goodsIssue.goodsId = goodsId;
			goodsIssue.id = id;
			goodsIssue.deptid = deptid
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mallm/goods/goodsIssue/add').reply(config => {
      var params = JSON.parse( config.data );
      let { question,answer,goodsId,id,deptid } = params;
      _GoodsIssues.push({
		question:question,
		answer:answer,
		goodsId:goodsId,
		id:id,
		deptid:deptid
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};